package umls.expander;

//import gnu.getopt.Getopt;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import jdbc.UMLS_DB_Interface;

public class Keywords {
/*
	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		String semtypeconfilename = "";
		String username = "abate";
		String password = "7prova7";
		String dbname = "umls";
		// String hostname = "jdbc:mysql://192.168.168.131:3306/";
		String hostname = "jdbc:mysql://bioeda2.polito.it:3306/";

		TreeMap<String, String> ExpandedGenes = new TreeMap<String, String>();
		TreeMap<String, String> ExpandedBiologicalProcesses = new TreeMap<String, String>();

		ArrayList<String> EG = new ArrayList<String>();

		Keyword keygenerator = new Keyword();

		String[] genes = null;
		String[] processes = null;

		Getopt g = new Getopt("Keywords", args, "g:p:c:");
		//
		int c;
		String arg;
		while ((c = g.getopt()) != -1) {
			switch (c) {
			case 'g':
				arg = g.getOptarg();
				genes = arg.split(":");
				break;

			case 'p':
				arg = g.getOptarg();
				processes = arg.split(":");
				break;
			case 'c':
				arg = g.getOptarg();
				semtypeconfilename = arg;
				break;
			//
			case '?':
				System.exit(1);
				break; // getopt() already printed an error

			//
			default:
				System.exit(1);
				System.out.println("default");
			}
		}

		UMLS_DB_Interface umlsdb = new UMLS_DB_Interface();

		umlsdb.SetUpConnection(hostname, username, password, dbname);

		SemGroupConfReader sgcr = new SemGroupConfReader(semtypeconfilename);

		sgcr.ReadAllowedSemanticGroups();

		sgcr.ReadAllowedSemanticTypes();

		for (int i = 0; i < genes.length; i++) {
			if (genes != null) {
				ExpandedGenes.put(genes[i], keygenerator
						.GetKeywordBySingleBiologicalProcessFilteredBySemType(
								genes[i], umlsdb, sgcr));
				// System.out.println("Dentro il modulo di espansione: "+ExpandedGenes.size()+" geni inseriti");
				// ExpandedGenes.put(genes[i],
				// keygenerator.GetKeywordBySingleGene(genes[i], umlsdb));
			}
		}

		for (int i = 0; i < processes.length; i++) {
			if (processes != null) {
				ExpandedBiologicalProcesses.put(processes[i], keygenerator
						.GetKeywordBySingleBiologicalProcessFilteredBySemType(
								processes[i], umlsdb, sgcr));
				System.out.println("Dentro il modulo di espansione: "
						+ ExpandedBiologicalProcesses.size()
						+ " processi inseriti");
				// ExpandedBiologicalProcesses.put(processes[i],
				// keygenerator.GetKeywordBySingleBiologicalProcess(processes[i],
				// umlsdb));
			}
		}

		// System.out.print(PrintMethod1(ExpandedGenes,
		// ExpandedBiologicalProcesses, EG));

		System.out.print(PrintMethod2(processes[0], ExpandedGenes,
				ExpandedBiologicalProcesses));

		umlsdb.CloseConnection();
	}
*/
	
	
	public static String PrintMethod2(String bioproc,
			TreeMap<String, String> ExpandedGenes,
			TreeMap<String, String> ExpandedBiologicalProcesses) {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		ArrayList<String> EP = new ArrayList<String>();

		// get all biological process expanded term
		Set bioproc_set = ExpandedBiologicalProcesses.entrySet();

		Iterator bioproc_it = bioproc_set.iterator();

		while (bioproc_it.hasNext()) {
			Map.Entry<String, String> entrybioproc = (Map.Entry<String, String>) bioproc_it
					.next();

			String[] EP_splitted = entrybioproc.getValue().split("\n");

			for (int i = 0; i < EP_splitted.length; i++) {
				EP.add(EP_splitted[i]);
			}
		}

		// each EP is combined with each gene and process
		Iterator<String> genes_it = ExpandedGenes.keySet().iterator();

		while (genes_it.hasNext()) {
			String gene = genes_it.next();

			Iterator<String> EP_it = EP.iterator();
			while (EP_it.hasNext()) {
				System.out.println("EP_it.hasnext()");
				String EP_next = EP_it.next();
				// result.append("\"" + bioproc + "\"" + " " + "AND" + " " +"\""
				// + gene +"\"" + " " + "AND" + " " + "\"" + EP_it.next() + "\""
				// + NEW_LINE);
				/*
				 * result.append(bioproc + " " + "AND" + " " + gene + " " +
				 * "AND" + " " + EP_next + NEW_LINE);
				 * result.append("\tAlternative: " + bioproc + " " + "AND" + " "
				 * + EP_next + NEW_LINE); result.append("\tAlternative: " + gene
				 * + " " + "AND" + " " + EP_next + NEW_LINE);
				 */
			}
		}

		// result.append(NEW_LINE + "\n\n\nfase nuova\n\n\n" + NEW_LINE);

		// with the process, each gene is combined with each EG belonging to the
		// EG set of the current gene
		genes_it = ExpandedGenes.keySet().iterator();

		while (genes_it.hasNext()) {
			String gene = genes_it.next();

			String expandedgenes = ExpandedGenes.get(gene);

			String[] geneset = expandedgenes.split("\n");

			for (int i = 0; i < geneset.length; i++) {
				// result.append("\"" + bioproc + "\"" + " " + "AND" + " " +"\""
				// + gene +"\"" + " " + "AND" + " " + "\"" + geneset[i] + "\"" +
				// NEW_LINE);
				result.append(bioproc + " " + "AND" + " " + gene + " " + "AND"
						+ " " + geneset[i] + NEW_LINE);
				result.append("\tAlternative: " + bioproc + " " + "AND" + " "
						+ geneset[i] + NEW_LINE);
				result.append("\tAlternative: " + gene + " " + "AND" + " "
						+ geneset[i] + NEW_LINE);
			}
		}

		return result.toString();
	}

	public static String PrintMethod1(TreeMap<String, String> ExpandedGenes,
			TreeMap<String, String> ExpandedBiologicalProcesses,
			ArrayList<String> EG) {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		// getting all the Expanded Gene as strings EG

		Set gene_set = ExpandedGenes.entrySet();

		Iterator gene_it = gene_set.iterator();

		while (gene_it.hasNext()) {
			Map.Entry<String, String> entrygene = (Map.Entry<String, String>) gene_it
					.next();

			String gene = entrygene.getKey();

			String[] EG_splitted = entrygene.getValue().split("\n");

			for (int i = 0; i < EG_splitted.length; i++) {
				EG.add(EG_splitted[i]);
			}
		}

		Set bioproc_set = ExpandedBiologicalProcesses.entrySet();

		Iterator bioproc_it = bioproc_set.iterator();

		while (bioproc_it.hasNext()) {
			Map.Entry<String, String> entrybioproc = (Map.Entry<String, String>) bioproc_it
					.next();

			String bioproc = entrybioproc.getKey();

			String[] EP = entrybioproc.getValue().split("\n");

			for (int i = 0; i < EP.length; i++) {
				Iterator<String> EGj = EG.iterator();

				while (EGj.hasNext()) {

					System.out.println("\"" + bioproc + "\"" + " " + "AND"
							+ " " + "\"" + EP[i] + "\"" + " " + "AND" + " "
							+ "\"" + EGj.next() + "\"");
				}
			}
		}

		return result.toString();
	}

	public static String ExpandGenes(String[] genes, Keyword keygen,
			UMLS_DB_Interface umlsdb) throws SQLException {
		String result = "";

		for (int i = 0; i < genes.length; i++) {
			String gene = genes[i];

			result = keygen.GetKeywordBySingleGene(gene, umlsdb);
		}

		return result;
	}

}